﻿(function($){
	
	function createMenu(target){
		var opts = $.data(target, 'frameMenu').options;	
		$(target).addClass('frameMenu');
		$(target).css('width',opts.width);			
		var twoCss = ' style="width:' + (opts.width-8) + 'px;" ';
		var menuspCss = ' style="width:' + (opts.width-20) + 'px;" ';
		
		var html='';
		$.getJSON(opts.url,function(data){
			html += '<ul>';
			for(var i=0;i<data.length;i++){
				html += '<li><div ';
				if(i==0){
					html += ' id="def" ';
				}else{
					html += ' id="'+data[i].iconCls+'" ';
				}
				html += 'class="menutitle"><img src="../../images/index/m_'+data[i].iconCls+'.png"/><span>'+data[i].text+'</span></div><div class="lm"><div class="lmt"></div><div class="lmc"></div><div class="lmb"></div></div><ul class="two"'+ twoCss + '>';
				
				if (data[i].children)
					for(var j=0;j<data[i].children.length;j++){
						if(data[i].children[j].children && data[i].children[j].children.length>0){
							html += '<li class="twoli"><div class="menuse menuse2" ><span style="float:left;">'+data[i].children[j].text+'</span>';
							if(data[i].children[j].count && data[i].children[j].count>0){
								html += '<span class="count">('+data[i].children[j].count+')</span>';
							}
							html += '<div class="sign"></div></div><ul class="three">';
							if (data[i].children[j].children)
								for(var k=0;k<data[i].children[j].children.length;k++){
									var node = data[i].children[j].children[k];
									node = $.extend({
										open_type: opts.open_type,
										target: opts.target								
									}, node);
									html += '<li>'+wrapMarkUp(node)+'</li>';
								}
							html += '</ul></li><li class="splitli"><div class="menusp"' + menuspCss + '></div></li>';
						}else{
							var node = data[i].children[j];
							node = $.extend({
								open_type: opts.open_type,
								target: opts.target								
							}, node);
							html += '<li class="twoli"><div class="menuse ">'+wrapMarkUp(node);
							if(data[i].children[j].count && data[i].children[j].count>0){
								html += '<span class="count">('+data[i].children[j].count+')</span>';
							}
							html += '</div></li><li class="splitli"><div class="menusp"' + menuspCss + '></div></li>';
						}
					}
				html += '</ul></li>';
			}
			html += '</ul>';
			
			$('#'+opts.menu_id).html(html);
			
			bindMenuItemEvent(target);
			openMenu('#def',target);
			if (opts.onLoadSuccess.call(target) == false) return;
									
		});
		/*
		if (opts.open_type){
			var context_id = opts.context_id;
			//将左侧菜单元素改造为能产生选项卡的元素
			$(target).find('a[target="main"]').each(function(){
				var url = $(this).attr('href');
				var text = $(this).text();
				$(this).attr('href','#');
				$(this).onclick(showTab(text, url));
			});
		}*/
	}
	
	function wrapMarkUp(node){
		var open_type = node.open_type;
		var url = node.url || "javascript:;";
		var id = node.target;
		var title = node.text;
		
		if (open_type=='frame')
			return '<a href="'+url+'" target="'+ id +'" >'+title+'</a>';
		else if (open_type == 'tab')
			return '<a href="#" onclick="showTab(\''+id+'\',\''+title+'\',\''+url+'\');" >'+title+'</a>';
		else
			return '<a onclick="window.open(\''+ url +'\',\''+ title +'\')">'+title+'</a>';
	}
	
	function hasthree(data){
		if (data.children)
			for(var z=0;z<data.children.length;z++){
				if(data.children[z].children && data.children[z].children.length > 0){
					return true;
				}
			}
		return false;
	}
	
	function bindMenuItemEvent(target){
		$(target).find('.menutitle').each(function(index,value){
			$(value).bind('click',function(){
				openMenu(value,target);
			 });
		});
		
		$(target).find('.menuse2').each(function(index,value){
			$(value).bind('click',function(){
				openSecond(value,target);
			 });
		});
		/*
		$(target).find('.two a').each(function(index,value){
			$(value).bind('click',function(){
				setColor(value);
			 });
		});
		*/
	}
	
	function setColor(o){
		$('.two').find('a').each(function(index,value){
			$(value).css('color', '#333333');
		});
		$(o).css('color', '#0099cc');
	}
	
	function openMenu(o, target){
		var panel = $(target);
		var pt = $(o).parent();
		
		if(pt.find('.two').css("display")=="block"){
			panel.find('.lm').hide();
			panel.find('.two').hide();
		}else{
			panel.find('.lm').hide();
			panel.find('.two').hide();
			pt.find('.two').show();
			pt.find('.lm').show();
			
			var h=panel.height() - panel.find(".two").length * panel.find(".menutitle").height();
			
			if(pt.find(".two").height()<=h){
				if((pt.find(".two").height()-90) >= 0){
					pt.find(".lmc").height(pt.find(".two").height()-82);					
				}else{
					pt.find(".lmc").height(0);
					//pt.find(".lm").height(97);
				}
			}else{
				pt.find(".lmc").height(h-97);
				pt.find(".two").height(h-15);
			}
			
			pt.find(".two").css("top",$(o).offset().top+60-110);
			pt.find(".two").css("left",$(o).offset().left+3);
		}
	}
	
	function openSecond(o, target){
		var panel = $(target);
		var _h=0;
		var _pt = $(o).parents(".two");
		
		if($(o).next().css("display")=="none"){
			$(o).css("background", "url(../../images/index/bg3.jpg) repeat-x 1px 1px");
			$(o).find(".sign").css("background","url(../../images/index/open.jpg) no-repeat");
			
			$(o).next().css("display","block");
			$(o).parent().height($(o).parent().find(".three li").length * $(o).parent().find(".three li").height() + $(o).height());
			
			_pt.find(".twoli").each(function(index,value){
				_h+=$(value).height();
			});
			if ($.browser.msie){
				_pt.height(_h + _pt.find(".twoli").length*1 + 20);
			}else{
				_pt.height(_h + _pt.find(".twoli").length*1);
			}		
		}else{
			$(o).css("background", "");
			$(o).find(".sign").css("background","url(../../images/index/close.jpg) no-repeat");
			
			$(o).next().css("display","none");
			$(o).parent().height(24);
			_pt.find(".twoli").each(function(index,value){
				_h+=$(value).height();
			});
			if ($.browser.msie){
				_pt.height(_h + _pt.find(".twoli").length * 1 + 20);
			}else{
				_pt.height(_h + _pt.find(".twoli").length * 1);
			}
		}
		
		var h=panel.height() - panel.find(".two").length * panel.find(".menutitle").height();
		
		if(_pt.height()<=h){
			if((_pt.height()-90) >= 0){
				_pt.parent().find(".lmc").height(_pt.height()-85);
			}else{
				_pt.parent().find(".lmc").height(0);
				//_pt.parent().find(".lm").height(97);
			}
		}else{
			_pt.parent().find(".lmc").height(h-97);
			_pt.height(h-16);
		}
	}		
	
	$.fn.frameMenu = function(options, param){
		if (typeof options == 'string'){
			return $.fn.frameMenu.methods[options](this, param);
		}
		
		options = options || {};
		return this.each(function(){
			var state = $.data(this, 'frameMenu');
			if (state){
				$.extend(state.options, options);
			} else {
				state = $.data(this, 'frameMenu', {
					options: $.extend({}, $.fn.frameMenu.defaults, $.fn.frameMenu.parseOptions(this), options)
				});
				createMenu(this);
			}
		});
	};
	
	$.fn.frameMenu.methods = {
		options: function(jq){
			return $.data(jq[0], 'frameMenu').options;
		}
	};
	
	$.fn.frameMenu.parseOptions = function(target){
		return $.extend({}, $.parser.parseOptions(target, ['url','menu_id']));
	};
	
	$.fn.frameMenu.defaults = {
		width:200,
		url:null,
		menu_id:null,
		open_type:'tab',
		target:null,
		onLoadSuccess:function(){}
	};
	
	$.parser.plugins.push("frameMenu"); 
})(jQuery);

//根据标题title和链接url显示一个选项卡到中部区域中id值为参数id的tab组件。如果同标题的选项卡存在，则选择此选项卡。否则新增。
function showTab(id, title, url){
	if ($('#'+id).tabs('exists',title)){
		$('#'+id).tabs('select',title);
	} else {
		$('#'+id).tabs('add',{
			title: title,
			type: 'frame',
			href: url,
			height: '100%',
			closable:true
		});
	}
}